package com.itheima.controller;

import com.itheima.Service.OrderSetService;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.pojo.OrdersetDEV;
import com.itheima.pojo.OrdersetTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/ordersetting")
public class OrderSetController {
    @Autowired
    private OrderSetService orderSetService;
    /**
     * 模板下载
     */
    @GetMapping("/download")
    public void DOwlodfile(String filename, HttpServletResponse response){
            log.info("文件下载");
            orderSetService.DOwlodfile(filename,response);
    }

    /**
     * 根据日期修改可预约人数
     */
    @PostMapping("/editNumberByOrderDate")
    public Result dateorderset(@RequestBody OrdersetDEV ordersetDEV){
        try {
            log.info("设置预约人:{}",ordersetDEV);
            orderSetService.upadte(ordersetDEV);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }
    /**
     * 根据年月查询对应的预约设置信息
     */
    @GetMapping("getOrderSettingByMonth")
    public Result list(String month){
        log.info("根据日期查询预约列表:{}",month);
        if (month != null & month.length() == 6 & Character.isDigit(month.charAt(5))) {
            month = month.substring(0, 5) + "0" + month.substring(5);
        }
        List<OrdersetTO> ARR =orderSetService.list(month);
        return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,ARR);
    }

    /**
     * 文件上传
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile){
            log.info("文件上传：{}",excelFile);
            orderSetService.insert(excelFile);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS,null);
    }
}
